package com.example.demo.controller;

import com.example.demo.entity.InstrumentCollection;
import com.example.demo.entity.TheoryKnowledgeCollection;
import com.example.demo.service.InstrumentCollectionService;
import com.example.demo.service.TheoryKnowledgeCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("v1")
public class TheoryKnowledgeCollectionController {

    @Autowired
    private TheoryKnowledgeCollectionService theoryKnowledgeCollectionService;

    // 添加收藏
    @PostMapping("/tk/add")
    public Map<String, Object> addCollection(@RequestBody TheoryKnowledgeCollection theoryKnowledgeCollection) {
        boolean success = theoryKnowledgeCollectionService.addCollection(theoryKnowledgeCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "收藏成功" : "收藏失败");
        return response;
    }

    // 删除收藏
    @DeleteMapping("/tk/delete")
    public Map<String, Object> deleteCollection(@RequestBody TheoryKnowledgeCollection theoryKnowledgeCollection) {
        boolean success = theoryKnowledgeCollectionService.deleteCollection(theoryKnowledgeCollection);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "取消收藏成功" : "取消收藏失败");
        return response;
    }

    // 获取用户的收藏列表
    @GetMapping("/tk/{userId}")
    public List<TheoryKnowledgeCollection> getCollectionsByUserId(@PathVariable Integer userId) {
        return theoryKnowledgeCollectionService.getCollectionsByUserId(userId);
    }

    // 检查是否收藏
    @GetMapping("/tk/check")
    public Map<String, Object> isCollected(@RequestParam Integer userId, @RequestParam Integer theoryKnowledgeId) {
        boolean collected = theoryKnowledgeCollectionService.isCollected(userId, theoryKnowledgeId);
        Map<String, Object> response = new HashMap<>();
        response.put("collected", collected);
        return response;
    }

    @PostMapping("/tk/{theoryKnowledgeId}")
    public ResponseEntity<?> ViewNote(@PathVariable Integer theoryKnowledgeId) {
        theoryKnowledgeCollectionService.clickViewCount(theoryKnowledgeId); // 使用注入的实例调用方法
        return ResponseEntity.ok().build();
    }

}
